/****
 * 提问
 ****/
;(function() {

	var E = window.wangEditor;
    var editor = new E('#editor');
    var editornum = 20000;
	var module = {
		eventListner: function () {
			$("#editor div[contenteditable='true']").on("input", function () {
				if (editor.txt.text().length > editornum) {
					var html = editor.txt.html();
					editor.txt.html(html.substring(0, html.lastIndexOf("</") - 1) + html.substring(html.lastIndexOf("</")));
				} 
				$(".editor-rest").text((editornum - editor.txt.text().length) + "/20000");
				
			})
			$("#btn-submit").click(function () {
				module.submitData("");
			});
			$("#btn-save").click(function () {
				module.submitData("");
			});
		},
		submitData: function (url, cb) {
			var	detail = editor.txt.html();
			if (!/>[\u4e00-\u9fa5]{1,}</.test(detail)) {
				alert("请输入详细解答内容");
				return false;
			}
			$.ajax({
				url: url,
				type: "post",
				data: {
					detail: detail
				},
				dataType: "json",
				success: function (data) {
					typeof cb === "function" && cb();
					location.reload();
				},
				error: function () {
					alert("提交解答内容失败");
				}
			})
		},
		intEditor: function () {
	        editor.customConfig.uploadImgShowBase64 = true;
	        // editor.customConfig.uploadImgServer = '/upload';
	        editor.create();
	        $("#editor div[contenteditable='true']").after("<span contenteditable='false' class='font12 gray editor-rest'>" + (editornum - editor.txt.text().length) + "/20000</span>");
		},
		init: function () {
			module.intEditor();

			module.eventListner();
		}
	};
	
	$(function () {
		module.init();
	});
}());